package ar.com.cema.methodology.analyzer.services.persistence;

import java.io.Serializable;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Hibernate implementation of {@link SimpleDAO}.
 *
 * @author Matias Suarez
 */
@Repository
@SuppressWarnings("rawtypes")
public final class SimpleDAOImpl implements SimpleDAO {
	@Autowired
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public Object save(Object obj) {
		Serializable id = getSession().save(obj);
		return getSession().get(obj.getClass(), id);
	}

	public void update(Object obj) {
		getSession().update(obj);
		getSession().flush();
	}

	public void saveOrUpdate(Object obj) {
		getSession().saveOrUpdate(obj);
		getSession().flush();
	}

	public Object merge(Object obj) {
		return getSession().merge(obj);
	}

	public void delete(Class theClass, Integer id) {
		Session session = getSession();
		Object obj = session.get(theClass, id);
		session.delete(obj);
	}

	public Object load(Class theClass, Serializable id) {
		return getSession().get(theClass, id);
	}

	public Query createQuery(String queryString) {
		return getSession().createQuery(queryString);
	}

	public Criteria createCriteria(Class persistentClass) {
		return getSession().createCriteria(persistentClass);
	}

	public Criteria createCriteria(Class persistentClass, String alias) {
		return getSession().createCriteria(persistentClass, alias);
	}

	public Query getNameQuery(String queryName) {
		return getSession().getNamedQuery(queryName);
	}

	public void delete(Object obj) {
		getSession().delete(obj);
	}

	public SQLQuery createSQLQuery(String queryString) {
		return getSession().createSQLQuery(queryString);
	}
}
